<?php

Yii::import('application.models._base.BaseAuctionPool');


class AuctionPool extends BaseAuctionPool
{
    public $category_id;
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	
	public function relations(){
	    return array(
	       'auctionInfo'       => array(self::BELONGS_TO,'AuctionInfo','','on' => 't.auction_id = auctionInfo.id'),
	       'auctionResource'   => array(self::MANY_MANY,'ResourceImg','auction_resource(auction_id,resource_id)'),
	       //'category'          => array(self::MANY_MANY,'Category','auction_category_relations(auction_id,category_id)','on' => 'category_category.is_delete = 0 '),
			'category'=>array(self::HAS_MANY,'AuctionCategoryRelations','','on'=>'t.auction_id=category.auction_id')
	    );
	}
	
	public static  function getRookieAuction($auctionId=null){
	    $rookie=0;
	    if ($auctionId!==null){
	        $param=new AuctionParame();
	        $param=$param->findByAttributes(array(
	            'auction_id'=>$auctionId,
	            'parame_name'=>'rookie'
	        ));
	        if (!empty($param)){
	            $rookie=$param->rookie;
	        }
	        return $rookie;
	    }
	}
	
	public function behaviors(){
	    return array(
	        'CAuctionCacheBehavior' => array(
	            'class'    => 'application.models.behaviors.CAuctionCacheBehavior',
	            'auctionIdAttribute'       =>  'auction_id',
	            'auctionStatusAttribute'   =>  'status',
	        )
	    );
	}
	
	protected function afterSave(){
	    if (!empty($this->status)){
	        $auctionInfo = new AuctionInfo();
	        $auctionInfo->updateAll(array('status' => $this->status),'id = :auction_id',array(
	            ':auction_id' => $this->auction_id
	        ));
	    }	    
	    parent::afterSave();
	}
	
	public function searchShelf($limit,$endTime,$noAuctionIds = array(),$demand){
	    $criteria = new CDbCriteria;
	    $criteria->with = 'auctionInfo';
	    $criteria->together = true;
	    if (!empty($noAuctionIds)){
	        $criteria->addNotInCondition('auction_id', $noAuctionIds);
	    }
	    
	    $criteria->addCondition('t.end_time >= '.$endTime);
	    $criteria->compare('t.status',1);
	    $criteria->limit = $limit;
	    $criteria->order = 't.end_time ASC';
	    return new CActiveDataProvider($this, array(
	        'criteria' => $criteria,
	        'pagination'=>array(
	            'pageSize'=>$limit,
	        ),
	    ));
	}
	
	public function searchDetail(){
		$criteria = new CDbCriteria;
		$criteria->with = array('category','auctionInfo','auctionResource');
		$criteria->together = true;
		if (!empty($this->auction_id)){
			$criteria->compare('t.auction_id', $this->auction_id);
		}
		return $this->find($criteria);
	}
	
	public function searchRun() {
	    $criteria = new CDbCriteria;
	    $criteria->with = array('category','auctionInfo','auctionResource');
	    //$criteria->together = true;	  
	    
	    if (!empty($this->category_id)){
	        switch ($this->category_id){
	            case 'live':
	                $criteria->compare('t.status',1);
	                break;
	            case 'End':
	                $criteria->compare('t.status',2);
	                break;
	            default:
	                $criteria->compare('t.status',1);
	                $criteria->compare('category.category_id',$this->category_id);
	                break;
	        }
	    }else{
	    	$criteria->compare('t.status',1);
	    }
	    $criteria->order = 't.end_time ASC';
	    
	    return new CActiveDataProvider($this, array(
	        'criteria' => $criteria,
	        'pagination'=>array(
	            'pageSize'=>5,
	        ),
	    ));
	}
}